package com.xu.system.service.db.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xu.system.mapper.SysDeptMapper;
import com.xu.system.model.entity.SysDept;
import com.xu.system.service.db.ISysDeptService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author 许乾伟
 * @since 2024-12-03 03:53:29
 */
@Service
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements ISysDeptService {

    @Override
    public Map<Long, String> getDeptNameMap(List<Long> deptIds) {
        QueryWrapper<SysDept> wrapper = new QueryWrapper<>();
        wrapper.eq(SysDept.IS_DELETE, Boolean.FALSE);
        if (CollectionUtils.isEmpty(deptIds)){
            wrapper.in(SysDept.ID,deptIds);
        }
        return list(wrapper).stream().collect(Collectors.toMap(SysDept::getId,SysDept::getDeptName));
    }

    @Override
    public List<SysDept> getChildDeptList(Long deptId) {
        List<SysDept> result = new ArrayList<>();
        QueryWrapper<SysDept> wrapper = new QueryWrapper<>();
        wrapper.eq(SysDept.PARENT_ID,deptId);
        wrapper.eq(SysDept.IS_DELETE, Boolean.FALSE);
        List<SysDept> list = list(wrapper);
        if (CollectionUtils.isEmpty(list)){
            return result;
        }
        for (SysDept sysDept : list) {
            result.add(sysDept);
            List<SysDept> childDeptList = getChildDeptList(sysDept.getId());
            if (!CollectionUtils.isEmpty(childDeptList)){
                result.addAll(childDeptList);
            }
        }
        return result;
    }
}
